//
//  ChoiceCarInfoViewController.m
//  CarCare
//
//  Created by ileo on 14-9-19.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "ChoiceCarInfoViewController.h"
#import "ChoiceCarTableViewController.h"
#import "UserHelper.h"
#import "NetCar.h"

#define CELLHEIGHT 50
#define SECTION_HEIGHT (11)

@interface ChoiceCarInfoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIButton *nextButton;

@property (nonatomic, strong) SetCarHelper *setHelper;

@property (nonatomic , strong) CarInfoModel * carInfoModel;

@end

@implementation ChoiceCarInfoViewController

- (instancetype)initWithTableRefreshType:(TYPE_TABLE_REFRESH)type carModel:(CarInfoModel *)carInfoModel
{
    self = [super initWithTableRefreshType:type];
    if (self) {
        self.carInfoModel = carInfoModel;
        self.setHelper = [[SetCarHelper alloc] init];
        self.setHelper.carInfoModel = carInfoModel;
//        self.setHelper.carBrand = self.carInfoModel.brandName;
//        self.setHelper.carModel = self.carInfoModel.modelName;
//        self.setHelper.carYear = self.carInfoModel.year;
//        self.setHelper.carStyle = self.carInfoModel.styleName;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"选择车型";
    
    
    
    self.tableView.frame = CGRectMake(0, SECTION_HEIGHT, __MAIN_WIDTH, 4 * CELLHEIGHT);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [Theme lineColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    
    __weak __typeof(self) wself = self;
    self.nextButton = [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(__MAIN_HEIGHT_64 - BOTTOM_SIDE) title:@"确定" inView:self.view action:^{
        [wself clickOnNext];
    }];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)clickOnNext{
    
    __weak __typeof(self) wself = self;

    [NetCar carImgWithStyleID:self.setHelper.carInfoModel.carTypeId success:^(NSString *image) {
        wself.setHelper.carInfoModel.modelImg = [image isKindOfClass:[NSNull class]] ? @"" : image;
        wself.carInfoModel = wself.setHelper.carInfoModel;
        [wself.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELLHEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = FONT_TABLE_TITLE;
        cell.textLabel.textColor = [Theme wordColor];
        UILabel *label = [Factory labelWithFrame:CGRectMake(100, 0, __MAIN_WIDTH - 130, CELLHEIGHT) font:FONT_DEFAULT_Light(15) text:@"" textColor:[[Theme wordColor] colorWithAlphaComponent:0.6] onView:cell textAlignment:NSTextAlignmentRight];
        label.numberOfLines = 1;
        label.tag = 17;
    }
    UILabel *label = (UILabel *)[cell viewWithTag:17];
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"选择品牌";
            label.text = self.setHelper.carInfoModel.brandName;
            break;
        case 1:
            cell.textLabel.text = @"选择车系";
            label.text = self.setHelper.carInfoModel.modelName;
            break;
        case 2:
            cell.textLabel.text = @"选择年份";
            label.text = self.setHelper.carInfoModel.year;
            break;
        case 3:
            cell.textLabel.text = @"选择车型";
            label.text = self.setHelper.carInfoModel.styleName;
            break;
            
        default:
            break;
    }
    if (![label.text isNotNil]) {
        label.text = LOCSTRING(@"unchoice_car");
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak __typeof(self) wself = self;
    switch (indexPath.row) {
        case 0:
        {
            [self.setHelper getCarBrandSuccess:^(NSDictionary *brands) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:brands type:CHOICE_CAR_BRAND setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        case 1:
        {
            [self.setHelper getCarModelSuccess:^(NSArray *models) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:models type:CHOICE_CAR_MODEL setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        case 2:
        {
            [self.setHelper getCarYearSuccess:^(NSArray *years) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:years type:CHOICE_CAR_YEAR setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        case 3:
        {
            [self.setHelper getCarStyleSuccess:^(NSArray *styles) {
                ChoiceCarTableViewController *vc = [[[ChoiceCarTableViewController alloc] initWithData:styles type:CHOICE_CAR_STYLE setCarHelper:wself.setHelper] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
                [wself.navigationController pushViewController:vc  animated:YES];
            }];
        }
            break;
        default:
            break;
    }
}


@end